package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.AssesRuleSystem;
import com.ruoyi.system.domain.PointsRuleSystem;
import com.ruoyi.system.mapper.AssesRuleSystemMapper;
import com.ruoyi.system.mapper.PointsRuleSystemMapper;
import com.ruoyi.system.service.IAssesRuleSystemService;
import com.ruoyi.system.service.IPointsRuleSystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.IntStream;

/**
 * 系统考核规则Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-22
 */
@Service
public class AssesRuleSystemServiceImpl implements IAssesRuleSystemService
{
    @Autowired
    private AssesRuleSystemMapper assesRuleSystemMapper;

    /**
     * 查询系统考核规则
     *
     * @param id 系统考核规则主键
     * @return 系统考核规则
     */
    @Override
    public AssesRuleSystem selectAssesRuleSystemById(Long id)
    {
        return assesRuleSystemMapper.selectAssesRuleSystemById(id);
    }

    /**
     * 查询系统考核规则列表
     *
     * @param assesRuleSystem 系统考核规则
     * @return 系统考核规则
     */
    @Override
    public List<AssesRuleSystem> selectAssesRuleSystemList(AssesRuleSystem assesRuleSystem)
    {
        List<AssesRuleSystem> assesRuleSystems = assesRuleSystemMapper.selectAssesRuleSystemList(assesRuleSystem);
        IntStream.range(0,assesRuleSystems.size()).forEach(
                i->{
                    int index = i+1;
                    AssesRuleSystem assesRuleSystemVo = assesRuleSystems.get(i);
                    assesRuleSystemVo.setOederNum(index);
                });
        return assesRuleSystems;
    }

    /**
     * 新增系统考核规则
     *
     * @param assesRuleSystem 系统考核规则
     * @return 结果
     */
    @Override
    public AjaxResult insertAssesRuleSystem(AssesRuleSystem assesRuleSystem)
    {
        Long assesRuleId = assesRuleSystem.getAssesRuleId();
        String ruleType = assesRuleSystem.getRuleType();
        String firstCondition = assesRuleSystem.getFirstCondition();
        String cyclicalPattern = assesRuleSystem.getCyclicalPattern();
        String modified = assesRuleSystem.getModified();
        AssesRuleSystem ruleSystem = new AssesRuleSystem();
        ruleSystem.setAssesRuleId(assesRuleId);
        ruleSystem.setRuleType(ruleType);
        ruleSystem.setFirstCondition(firstCondition);
        ruleSystem.setCyclicalPattern(cyclicalPattern);
        ruleSystem.setModified(modified);
        List<AssesRuleSystem> assesRuleSystems = assesRuleSystemMapper.selectAssesRuleSystemList(ruleSystem);
        if(!CollectionUtils.isEmpty(assesRuleSystems)){
            return AjaxResult.error("不允许重复添加相同规则");
        }
        try {
            assesRuleSystemMapper.insertAssesRuleSystem(assesRuleSystem);
            return AjaxResult.success("添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.success("添加失败");
        }
    }

    /**
     * 修改系统考核规则
     *
     * @param assesRuleSystem 系统考核规则
     * @return 结果
     */
    @Override
    public AjaxResult updateAssesRuleSystem(AssesRuleSystem assesRuleSystem)
    {
        String ruleType = assesRuleSystem.getRuleType();
        String firstCondition = assesRuleSystem.getFirstCondition();
        String cyclicalPattern = assesRuleSystem.getCyclicalPattern();
        String modified = assesRuleSystem.getModified();
        if(StringUtils.isNotBlank(ruleType)&&StringUtils.isNotBlank(firstCondition)&&StringUtils.isNotBlank(cyclicalPattern)&&StringUtils.isNotBlank(modified)){
            AssesRuleSystem ruleSystemById = assesRuleSystemMapper.selectAssesRuleSystemById(assesRuleSystem.getId());
            AssesRuleSystem ruleSystem = new AssesRuleSystem();
            Long assesRuleId = ruleSystemById.getAssesRuleId();
            ruleSystem.setAssesRuleId(assesRuleId);
            ruleSystem.setRuleType(ruleType);
            ruleSystem.setFirstCondition(firstCondition);
            ruleSystem.setCyclicalPattern(cyclicalPattern);
            ruleSystem.setModified(modified);
            List<AssesRuleSystem> assesRuleSystems = assesRuleSystemMapper.selectAssesRuleSystemList(ruleSystem);
            if(!CollectionUtils.isEmpty(assesRuleSystems)){
                AssesRuleSystem ruleSystemVo = assesRuleSystems.get(0);
                Long id = ruleSystemVo.getId();
                if(!id.equals(assesRuleSystem.getId())){
                    return AjaxResult.error("不允许重复添加相同规则");
                }
            }
        }
        try {
            assesRuleSystemMapper.updateAssesRuleSystem(assesRuleSystem);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.success("修改失败");
        }
    }

    /**
     * 删除系统考核规则信息
     *
     * @param id 系统考核规则主键
     * @return 结果
     */
    @Override
    public int deleteAssesRuleSystemById(Long id)
    {
        return assesRuleSystemMapper.deleteAssesRuleSystemById(id);
    }
}
